package org.tema1;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailsActivity extends Activity {

	public static final String PATH = "http://www.google.com/ig/api?weather=";
	public ArrayList<ImageView> _icon;
	public ArrayList<TextView> _zi;
	public ArrayList<TextView> _minima;
	public ArrayList<TextView> _stare;

	public void AddCell(View view)
	{
		_icon.add((ImageView)view.findViewById(R.id.icon));
		_zi.add((TextView)view.findViewById(R.id.zi)); 
		_minima.add((TextView)view.findViewById(R.id.minima));
		_stare.add((TextView)view.findViewById(R.id.stare));
	}
	
	public void SetImage(String p, ArrayList<ImageView> _icon, int i)
	{

		if (p.equals("/ig/images/weather/mostly_sunny.gif"))
			_icon.get(i).setImageResource(R.drawable.mostly_sunny);
		else if (p.equals("/ig/images/weather/sunny.gif"))
			_icon.get(i).setImageResource(R.drawable.sunny);
		else if (p.equals("/ig/images/weather/partly_cloudy.gif"))
			_icon.get(i).setImageResource(R.drawable.partly_cloudy);
		else if (p.equals("/ig/images/weather/mostly_cloudy.gif"))
			_icon.get(i).setImageResource(R.drawable.mostly_cloudy);
		else if (p.equals("/ig/images/weather/chance_of_storm.gif"))
			_icon.get(i).setImageResource(R.drawable.chance_of_storm);
		else if (p.equals("/ig/images/weather/rain.gif"))
			_icon.get(i).setImageResource(R.drawable.rain);
		else if (p.equals("/ig/images/weather/chance_of_rain.gif"))
			_icon.get(i).setImageResource(R.drawable.chance_of_rain);
		else if (p.equals("/ig/images/weather/chance_of_snow.gif"))
			_icon.get(i).setImageResource(R.drawable.chance_of_snow);
		else if (p.equals("/ig/images/weather/mostly_sunny.gif"))
			_icon.get(i).setImageResource(R.drawable.mostly_sunny);
		else if (p.equals("/ig/images/weather/cloudy.gif"))
			_icon.get(i).setImageResource(R.drawable.cloudy);
		else if (p.equals("/ig/images/weather/mist.gif"))
			_icon.get(i).setImageResource(R.drawable.mist);
		else if (p.equals("/ig/images/weather/storm.gif"))
			_icon.get(i).setImageResource(R.drawable.storm);
		else if (p.equals("/ig/images/weather/thunderstorm.gif"))
			_icon.get(i).setImageResource(R.drawable.thunderstorm);
		else if (p.equals("/ig/images/weather/chance_of_tstorm.gif"))
			_icon.get(i).setImageResource(R.drawable.chance_of_tstorm);
		else if (p.equals("/ig/images/weather/sleet.gif"))
			_icon.get(i).setImageResource(R.drawable.sleet);
		else if (p.equals("/ig/images/weather/snow.gif"))
			_icon.get(i).setImageResource(R.drawable.snow);
		else if (p.equals("/ig/images/weather/icy.gif"))
			_icon.get(i).setImageResource(R.drawable.icy);
		else if (p.equals("/ig/images/weather/dust.gif"))
			_icon.get(i).setImageResource(R.drawable.dust);
		else if (p.equals("/ig/images/weather/fog.gif"))
			_icon.get(i).setImageResource(R.drawable.fog);
		else if (p.equals("/ig/images/weather/smoke.gif"))
			_icon.get(i).setImageResource(R.drawable.smoke);
		else if (p.equals("/ig/images/weather/haze.gif"))
			_icon.get(i).setImageResource(R.drawable.haze);
		else if (p.equals("/ig/images/weather/flurries.gif"))
			_icon.get(i).setImageResource(R.drawable.flurries);
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.details);
		
		TextView data = (TextView)findViewById(R.id.textView6);
		TextView stare = (TextView)findViewById(R.id.textView7);
		TextView temp = (TextView)findViewById(R.id.textView8);
		TextView umiditate = (TextView)findViewById(R.id.textView9);
		TextView vant = (TextView)findViewById(R.id.textView10);
		ImageView icon = (ImageView)findViewById(R.id.icon);
		
		_icon = new ArrayList<ImageView>();
		 _zi = new ArrayList<TextView>();
		_minima = new ArrayList<TextView>();
		_stare = new ArrayList<TextView>();

		AddCell((View)findViewById(R.id.cell1));
		AddCell((View)findViewById(R.id.cell2));
		AddCell((View)findViewById(R.id.cell3));
		AddCell((View)findViewById(R.id.cell4));

		Intent intent = getIntent ();
		Button close;

		close = (Button)findViewById(R.id.close);
		close.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});

		String nume_oras = intent.getStringExtra("nume_oras");
		String responseBody = "";
		HttpGet getMethod=new HttpGet(PATH+nume_oras);
		HttpClient client = new DefaultHttpClient ();
		try {
			
			
			ResponseHandler<String> responseHandler=new BasicResponseHandler();
			responseBody=client.execute(getMethod, responseHandler);
			// responseBody reprezinta continutul fisierului RSS luat de pe Internet
			
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(new ByteArrayInputStream(responseBody.getBytes("UTF-8")));
			doc.getDocumentElement().normalize();

			NodeList nodeList = null;
			NodeList nodeList2 = null;
			Element element = null;
			nodeList = doc.getElementsByTagName("forecast_information");
			if (nodeList.getLength() !=0)
			{
				element = (Element)nodeList.item(0);
				nodeList = element.getElementsByTagName("forecast_date");
				if (nodeList.getLength() !=0)
					data.setText(((Element)nodeList.item(0)).getAttribute("data"));
			}

			nodeList = doc.getElementsByTagName("current_conditions");
			if (nodeList.getLength() !=0)
			{
				element = (Element)nodeList.item(0);
				nodeList = element.getElementsByTagName("condition");
				if (nodeList.getLength() !=0)
					stare.setText(((Element)nodeList.item(0)).getAttribute("data"));

				nodeList = element.getElementsByTagName("temp_c");
				if (nodeList.getLength() !=0)
					temp.setText(((Element)nodeList.item(0)).getAttribute("data"));

				nodeList = element.getElementsByTagName("humidity");
				if (nodeList.getLength() !=0)
					umiditate.setText(((Element)nodeList.item(0)).getAttribute("data"));

				nodeList = element.getElementsByTagName("icon");
				if (nodeList.getLength() !=0)
				{
					ArrayList<ImageView> aux = new ArrayList<ImageView>();
					aux.add(icon);
					SetImage(((Element)nodeList.item(0)).getAttribute("data"), aux, 0);
				}

				nodeList = element.getElementsByTagName("wind_condition");
				if (nodeList.getLength() !=0)
					vant.setText(((Element)nodeList.item(0)).getAttribute("data"));
			}

			nodeList2 = doc.getElementsByTagName("forecast_conditions");
			for(int i=0;i<nodeList2.getLength();i++)
			{
				element = (Element)nodeList2.item(i);
				nodeList = element.getElementsByTagName("day_of_week");
				if(nodeList.getLength() != 0)
					_zi.get(i).setText(((Element)nodeList.item(0)).getAttribute("data"));

				nodeList = element.getElementsByTagName("low");
				if(nodeList.getLength() != 0){
					int aux = Integer.parseInt(((Element)nodeList.item(0)).getAttribute("data"));
					aux = (aux-32)*5/9;
					_minima.get(i).setText(Integer.toString(aux));
				}

				nodeList = element.getElementsByTagName("icon");
				if(nodeList.getLength() != 0)
					SetImage(((Element)nodeList.item(0)).getAttribute("data"), _icon, i);

				nodeList = element.getElementsByTagName("condition");
				if(nodeList.getLength() != 0)
					_stare.get(i).setText(((Element)nodeList.item(0)).getAttribute("data"));
			}
		}
		catch (Throwable t) {
			t.printStackTrace();
		}
	}

}